<?php if (!defined('THINK_PATH')) exit();?><!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?php echo ($CONF['mallTitle']); ?>后台管理中心</title>
    <link href="Public/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="Apps/Admin/View/css/AdminLTE.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="/fcgmall/Public/jquery-ui/css/base/jquery-ui-1.10.4.custom.min.css" />
    <!--[if lt IE 9]>
    <script src="Public/js/html5shiv.min.js"></script>
    <script src="Public/js/respond.min.js"></script>
    <![endif]-->
    <script src="Public/js/jquery.min.js"></script>
    <script src="Public/plugins/bootstrap/js/bootstrap.min.js"></script>
    <script src="Public/js/common.js"></script>
    <script src="Public/plugins/plugins/plugins.js"></script>
    <script src="/fcgmall/Public/jquery-ui/jquery-ui-1.10.4.custom.min.js"></script>
    <script src="/fcgmall/Public/js/common.js"></script>
    <style>
        .control-box{
            position: absolute;
            right: 15px;
            top: 5px;
        }

        .control-box a{
            margin-left: 10px;
        }

        #goodsSelected>span{
            margin:5px 10px;
        }

        #shopsSelected>span{
            margin:5px 10px;
        }

        #tGoodsList li{
            border-bottom: solid 1px grey;
        }

        #tGoodsList img{
            width:100px;
            height:100px;
        }

        .imgPreview img{
            width:50px;
            height:50px;
        }

        #shopsSelected,#goodsSelected {
            min-height: 50px;
        }


         .panel{
             border-radius: 0px;

         }

        .panel-heading{
            border-radius: 0px;
        }


        .wst-tab-box{width:100%; height:auto; margin:0px auto;}
        .wst-tab-nav{margin:0; padding:0; height:25px; line-height:24px;position: relative;top:2px;left:3px;}
        .wst-tab-nav li{cursor:pointer;float:left; margin:0 0px; list-style:none; border:1px solid #ddd; border-bottom:none; height:24px; width:100px; text-align:center; background:#eeeeee;color:#000000;}
        .wst-tab-nav .on{background:#ffffff;color:#000000;border-bottom:0 none;}
        .wst-tab-content{padding:5px;width:99%; height:auto; border:1px solid #ddd;background:#FFF;}
        .wst-gallery-imgs{width:770px;height:auto;}
        .wst-gallery-img{width:140px;height:100px;float:left;overflow:hidden;margin:10px 5px 5px 5px;}

    </style>


</head>
<body>


<!--<div class="panel panel-default hidden" id="selectShopBox" currentStep="1">-->
    <!--<div class="panel-heading">-->
        <!--<h3 class="panel-title">-->
            <!--选择商家-->
        <!--</h3>-->
        <!--<div class="pull-right control-box">-->
            <!--<a class="btn btn-default last-step hidden">上一步</a>-->
            <!--<a class="btn btn-default next-step">下一步</a>-->
        <!--</div>-->
    <!--</div>-->
    <!--<div class="panel-body">-->
        <!--<div class="panel panel-default">-->
            <!--<div class="panel-heading">-->

                <!--已选商家-->

            <!--</div>-->
            <!--<div class="panel-body" id="shopsSelected">-->

            <!--</div>-->
        <!--</div>-->
        <!--<form  id="searchShopForm">-->
            <!--<div class='wst-tbar'>-->
                <!--地区：<select id='areaId1' name='areaId1' onchange='javascript:getAreaList("areaId2",this.value,0)'>-->
                <!--<option value=''>请选择</option>-->
                <!--<?php if(is_array($areaList)): $i = 0; $__LIST__ = $areaList;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$vo): $mod = ($i % 2 );++$i;?>-->
                    <!--<option value='<?php echo ($vo["areaId"]); ?>' <?php if($areaId1 == $vo['areaId'] ): ?>selected<?php endif; ?>><?php echo ($vo['areaName']); ?></option>-->
                <!--<?php endforeach; endif; else: echo "" ;endif; ?>-->
            <!--</select>-->
                <!--<select id='areaId2' name='areaId2'>-->
                    <!--<option value=''>请选择</option>-->
                <!--</select>-->
                <!--商家名称：<input type='text' id='shopName' name='shopName' class='form-control wst-ipt-10' value='<?php echo ($shopName); ?>'/>-->
                <!--商家编号：<input type='text' id='shopSn' name='shopSn' class='form-control wst-ipt-10' value='<?php echo ($shopSn); ?>'/>-->
                <!--<button type="button" class="btn btn-primary glyphicon glyphicon-search" id="searchShopBtn">查询</button>-->
            <!--</div>-->
        <!--</form>-->
        <!--<div class='wst-body'>-->
            <!--<div>-->
                <!--商家列表-->
            <!--</div>-->
            <!--<table class="table table-hover table-striped table-bordered wst-list">-->
                <!--<thead>-->
                <!--<tr>-->
                    <!--<th width='80'>商家名称</th>-->
                    <!--<th width='80'>商家编号</th>-->
                    <!--<th width='80'>店主</th>-->
                    <!--<th width='80'>分类</th>-->
                    <!--<th width='20' class="text-center">操作</th>-->
                <!--</tr>-->
                <!--</thead>-->
                <!--<tbody  id="shopsListBody">-->
                <!--<?php if(is_array($Page['root'])): $i = 0; $__LIST__ = $Page['root'];if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$vo): $mod = ($i % 2 );++$i;?>-->
                    <!--<tr>-->
                        <!--<td style="display:none;"><?php echo ($vo['shopId']); ?></td>-->
                        <!--<td><?php echo ($vo['shopName']); ?></td>-->
                        <!--<td><?php echo ($vo['shopSn']); ?>&nbsp;</td>-->
                        <!--<td><?php echo ($vo['userName']); ?>&nbsp;</td>-->
                        <!--<td><?php echo ($vo['catName']); ?>&nbsp;</td>-->
                        <!--<td class="text-center"><a class="btn btn-primary glyphicon selectShops" >选择</a></td>-->
                    <!--</tr>-->
                <!--<?php endforeach; endif; else: echo "" ;endif; ?>-->
                <!--<tr>-->
                    <!--<td colspan='11' align='center'><?php echo ($Page['pager']); ?></td>-->
                <!--</tr>-->
                <!--</tbody>-->
            <!--</table>-->
        <!--</div>-->
    <!--</div>-->
<!--</div>-->




<div class="panel panel-default " id="packagePriceBox" currentStep="3">
    <div class="panel-body">
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>套餐商品列表</strong>
                <div class="pull-right"><a id="openGoodsModal" class="btn btn-default">添加<span class="glyphicon glyphicon-plus"></span></a></div>
            </div>
            <div class="panel-body">
                <ul class="media-list" id="tGoodsList">
                    <?php if(!empty($package)): if(is_array($goodsList)): $i = 0; $__LIST__ = $goodsList;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$goods): $mod = ($i % 2 );++$i;?><li class="media" goodsid="<?php echo ($goods['goodsId']); ?>" periodId="<?php echo ($goods['periodId']); ?>"><a class="pull-left" href="#"> <img class="media-object"
                                                                                                src="/fcgmall/<?php echo ($goods["goodsThums"]); ?>"> </a>

                                <div class="media-body">
                                    <div class="media-heading"> 商品名：<span><?php echo ($goods['goodsName']); ?></span> <a class="removePackageGoods pull-right text-danger"><span class="glyphicon glyphicon-remove"></span></a></div>
                                    <div class="media-heading">商品简介：<span><?php echo ($goods['$goodsSpec']); ?></span></div>
                                    <div class="media-heading goodsPrice">商品价格：<span><?php echo ($goods['periodId']?$goods['pPrice']:$goods['shopPrice']); ?></span></div>
                                    <div class="media-heading">套餐价格：<input type="text" class="tPrice" value="<?php echo ($goods['tPrice']); ?>"></div>
                                </div>
                            </li><?php endforeach; endif; else: echo "" ;endif; endif; ?>
                </ul>

            </div>
        </div>


        <div>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <strong>套餐属性</strong>

                </div>
                <div class="panel-body">
                    <div class="form-group">
                        套餐名称：<input name="tName" id="tName" value="<?php echo ($package["tName"]); ?>">
                    </div>
                    <div class="form-group">套餐时间：<input id="tStartTime" value="<?php echo ($package["starttime"]); ?>">-<input id="tEndTime" value="<?php echo ($package["endtime"]); ?>"></div>
                    <div class="form-group">套餐简介：
             <textarea id="tDesc" style="width: 100%;height: 200px;" >
                     <?php echo ($package["desc"]); ?>
            </textarea>
                    </div>
                </div>
            </div>



        </div>
    </div>
    <div class="panel-footer text-center"><a class="btn btn-default btn-lg" id="savePackage">保    存</a></div>
</div>




<!-- 模态框（Modal） -->
<div class="modal fade text-center" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"  aria-hidden="true" style="z-index: 2001">
    <div class="modal-dialog" style="display: inline-block; width: auto;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="priceSelect">确定</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>


<!--警告框-->

<div class="modal fade text-center" id="alertModal"    style="z-index: 2002">
    <div class="modal-dialog " style="display:inline-block;width:300px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">警告</h4>
            </div>
            <div class="modal-body">
                <p></p>
            </div>
            <div class="modal-footer">
                <a class="btn btn-success" data-dismiss="modal">确定</a>
            </div>
        </div>
    </div>
</div>


<!--商品选择窗口-->
<div class="modal text-center" id="goodsModal"  style="z-index: 2000;" data-backdrop="false">
    <div class="modal-dialog text-left" style="display: inline-block; width: 1000px;">
        <div class="modal-content">
            <div class="modal-body">



                        <div class="panel panel-default">
                            <div class="panel-heading">

                                已选商品

                            </div>
                            <div class="panel-body" id="goodsSelected">

                            </div>
                        </div>
                        <form method='post' action='<?php echo U("Admin/Goods/index");?>' autocomplete="off">
                            <div class='wst-tbar'>

                                <!--<select id='goodsCatId2' name='goodsCatId2' onchange='javascript:getCatList("goodsCatId3",this.value,1);'>-->
                                <!--<option value='0'>请选择</option>-->
                                <!--</select>-->
                                <!--<select id='goodsCatId3' name='goodsCatId3'>-->
                                <!--<option value='0'>请选择</option>-->
                                <!--</select>-->
                            </div>
                            <div class='wst-tbar'>
                                类别：<select name="shopCat" id="shopCat">
                                            <?php if(is_array($shopCatList)): $i = 0; $__LIST__ = $shopCatList;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$catItem): $mod = ($i % 2 );++$i;?><option value="<?php echo ($catItem['catId']); ?>"><?php echo ($catItem['catName']); ?></option><?php endforeach; endif; else: echo "" ;endif; ?>
                                      </select>
                                所属商家：<input type="text" id="shopName" name="shopName">


                                商品：<input type='text' id='goodsName' name='goodsName' />
                                <button id="searchGoodsBtn" type="button" class="btn btn-primary glyphicon glyphicon-search">查询</button>
                            </div>
                        </form>
                        <div class='wst-body'>

                            <table class="table table-hover table-striped table-bordered wst-list">
                                <thead>
                                <tr>



                                    <th width='180'>商品名称</th>
                                    <th width='60'>商品编号</th>
                                    <th width='80'>价格</th>
                                    <th width='100'>商家</th>
                                    <th width='100'>商城分类</th>
                                    <th width='40' class="text-center">操作</th>

                                </tr>
                                </thead>
                                <tbody id="goodsListBody">

                                <!--<tr>-->
                                <!--<td colspan='10' align='center'><?php echo ($Page['pager']); ?></td>-->
                                <!--</tr>-->
                                </tbody>
                            </table>
                        </div>


            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-default" onclick="$('#goodsModal').modal('hide');currentModal=null;tempGoodsSelectedList={}; $('#goodsSelected').empty();">关闭</button>
                <button type="button" class="btn btn-primary" id="addGoodsSelected">确定</button>
            </div>

        </div>
    </div>
</div>
    

</body>

<script>

    var shopsSelectedList = new Object();//选择的商家列表
    var goodsSelectedList = new Object();//选择的商品列表
    var tempGoodsSelectedList = new Object();//临时选择的商品列表
    var periodSelectedList = new Object();//选择的时间段列表
    var goodsIdDeleteList = new Array();//需要删除的商品列表
    var goodsTPriceEditList = new Object();




    var currentModal = null;
    $("#tStartTime").datepicker({
        showButtonPanel:false,
        monthNames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
        dayNamesMin: ['日','一','二','三','四','五','六'],
        prevText: '<上月',
        nextText: '下月>',
        currentText: '今天',
        dateFormat: 'yy-mm-dd'

    });

    $("#tEndTime").datepicker({
        showButtonPanel:false,
        monthNames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
        dayNamesMin: ['日','一','二','三','四','五','六'],
        prevText: '<上月',
        nextText: '下月>',
        currentText: '今天',
        dateFormat: 'yy-mm-dd'

    });



    /**
     * 检查当前要选择的商品是已选择
     * */
    function checkGooodsExist(goodsId){
            var result = false;;
            $("#tGoodsList li").each(function(){
                if($(this).attr("goodsId") == goodsId){
                    result = true;
                    return true;
                }
            });
        return result;
    }

   /**
    * 根据数字转成
    *
    * **/

    function numberToWeek(number){
       var weekDate = '';
       switch(number){
           case 1:
               weekDate = "星期一";
               break;
           case 2:
               weekDate = "星期二";
               break;
           case 3:
               weekDate = "星期三";
               break;
           case 4:
               weekDate = "星期四";
               break;
           case 5:
               weekDate = "星期五";
               break;
           case 6:
               weekDate = "星期六";
               break;
           case 7:
               weekDate = "星期日";
               break;

       }

       return weekDate;
    }



    // 弹出提示框
    function alertModal(content){
        $("#alertModal p").text(content);
        $("#alertModal").modal();
    }



    function getGoodsListAjax(p,callback){
        var p = arguments[0] ? arguments[0] : 1;
        $.post("<?php echo U('Admin/Package/getGoodsListByShopId');?>&p="+p,
                {
                    "shopId":$("#goodsShopName").val(),
                    "goodsName":$("#goodsName").val(),
                    "shopName":$("#shopName").val(),
                    "shopCat":$("#shopCat").val()

                },
                function(data){
                    var trStr = '';
                    var goodsList = data.data;
                    $("#goodsListBody").empty();
                    for(var i = 0;i < goodsList.length;i++){
                        var goodsTrObj = document.createElement("tr");
                        $(goodsTrObj).append("<td style='display: none;'>"+goodsList[i].goodsId+"</td>");
                        $(goodsTrObj).append(" <td img='"+goodsList[i].goodsThums+"' class='imgPreview'> <img src='/fcgmall/"+goodsList[i].goodsThums+"' width='50'/>"+goodsList[i].goodsName+" </td>");
                        $(goodsTrObj).append("  <td>"+goodsList[i].goodsSn+"&nbsp;</td>");
                        var priceStr = '';
                        var controlStr = '';
                        if(parseFloat(goodsList[i].shopPrice) > 0){
                            priceStr = goodsList[i].shopPrice;
                            controlStr = '<td class="text-center"><a class="btn btn-primary glyphicon selectGoods" >选择</a></td>'
                        }else{
                            priceStr = "未选择";
                            controlStr = '<td class="text-center"><a class="btn btn-primary glyphicon selectPeriod" style="margin-right:5px;" >时段</a><a class="btn btn-primary glyphicon selectGoods" >选择</a></td>'

                        }
                        $(goodsTrObj).append("<td>"+priceStr+"&nbsp;</td>");
                        $(goodsTrObj).append("<td>"+goodsList[i].shopName+"&nbsp;</td>");
                        $(goodsTrObj).append("<td>"+goodsList[i].catName+"&nbsp;</td>");
                        $(goodsTrObj).append("<td style='display:none;'>"+goodsList[i].goodsSpec+"&nbsp;</td>");


                        $(goodsTrObj).append(controlStr);
                        $("#goodsListBody").append(goodsTrObj);

                    }
                    $("#goodsListBody").append("<tr><td colspan='10' align='center'>"+data.page+"</td></tr>");
                    if(callback){callback();}

                }
        );
    }

    function getShopsListAjax(p,callback){
        var p = arguments[0] ? arguments[0] : 1;
        $.post("<?php echo U('Admin/Package/getShopListAjax');?>",
                {
                    shopName:$("#shopName").val(),
                    shopSn:$("#shopSn").val(),
                    areaId1:$("#areaId1").val(),
                    areaId2:$("#areaId2").val()
                },
                function(data){
                    var trStr = '';
                    var shopsList = data.data;
                    $("#shopsListBody").empty();
                    for(var i = 0;i <shopsList.length;i++){
                        var shopTrObj = document.createElement("tr");
                        $(shopTrObj).append("<td style='display: none;'>"+shopsList[i].shopId+"</td>");
                        $(shopTrObj).append(" <td > "+shopsList[i].shopName+" </td>");
                        $(shopTrObj).append(" <td > "+shopsList[i].shopSn+" </td>");
                        $(shopTrObj).append("  <td>"+shopsList[i].userName+"&nbsp;</td>");
                        $(shopTrObj).append("<td>"+shopsList[i].catName+"&nbsp;</td>");
                        $(shopTrObj).append('<td class="text-center"><a class="btn btn-primary glyphicon selectShops" >选择</a></td>');
                        $("#shopsListBody").append(shopTrObj);

                    }
                    $("#shopsListBody").append("<tr><td colspan='10' align='center'>"+data.page+"</td></tr>");
                    if(callback){
                        callback();
                    }


                }
        );
    }

    function del(id){
        Plugins.confirm({title:'信息提示',content:'您确定要删除该商家吗?',okText:'确定',cancelText:'取消',okFun:function(){
            Plugins.closeWindow();
            Plugins.waitTips({title:'信息提示',content:'正在操作，请稍后...'});
            $.post("<?php echo U('Admin/Shops/del');?>",{id:id},function(data,textStatus){
                var json = WST.toJson(data);
                if(json.status=='1'){
                    Plugins.setWaitTipsMsg({content:'操作成功',timeout:1000,callback:function(){
                        location.reload();
                    }});
                }else{
                    Plugins.closeWindow();
                    parent.showMsg({msg:'操作失败!',status:'danger'});
                }
            });
        }});
    }
    function getAreaList(objId,parentId,t,id){
        var params = {};
        params.parentId = parentId;
        $('#'+objId).empty();
        if(t<1){
            $('#areaId3').empty();
            $('#areaId3').html('<option value="">请选择</option>');
        }
        var html = [];
        $.post("<?php echo U('Admin/Areas/queryByList');?>",params,function(data,textStatus){
            html.push('<option value="">请选择</option>');
            var json = WST.toJson(data);
            if(json.status=='1' && json.list.length>0){
                var opts = null;
                for(var i=0;i<json.list.length;i++){
                    opts = json.list[i];
                    html.push('<option value="'+opts.areaId+'" '+((id==opts.areaId)?'selected':'')+'>'+opts.areaName+'</option>');
                }
            }
            $('#'+objId).html(html.join(''));
        });
    }


    /**
     * 商品列表分页
     */
    $("#goodsListBody").on("click",".wst-ajax-pagination a",function(){
        getGoodsListAjax($(this).attr("p"));
    });

    /**
     * 商家列表分页
     */

    $("#shopsListBody").on("click",".wst-ajax-pagination a",function(){
        getShopsListAjax($(this).attr("p"));
    });

    /**
     * 搜索商品按钮
     */
    $("#searchGoodsBtn").click(function(){
        getGoodsListAjax();
    });

    /**
     * 选择商家获取商品类表
     */
    $("#goodsShopName").change(function(){
        getGoodsListAjax();
    });

    /**
     * 选择商品
     */
    $("#goodsListBody").on("click",".selectGoods",function(){
        var goodsPrice  = null;
        var periodId = 0;
        var goodsId = $(this).parent().siblings().eq(0).text();

        if(checkGooodsExist(goodsId)){
            alertModal(" 该商品已选择");
            return false;
        }

        if($.isEmptyObject(periodSelectedList[goodsId])){
            goodsPrice = parseFloat($(this).parent().siblings().eq(3).text());
            if(isNaN(goodsPrice)){
                alertModal("该商品价格分不同时段，请选择");
                return false;
            }
        }else{
            goodsPrice = periodSelectedList[goodsId].periodPrice;
            periodId = periodSelectedList[goodsId].priceId;
        }

        if(!tempGoodsSelectedList[goodsId] &&!goodsSelectedList[goodsId]){
            var object = new Object();
            var goodsName = $(this).parent().siblings().eq(1).text();
            object.goodsName = goodsName;
            var shopName = $("#goodsShopName").text();
            object.shopName = shopName;

            object.goodsPrice = goodsPrice;
            var thumb = $(this).parent().siblings().eq(1).children("img").attr("src");
            object.goodsThums = thumb;
            var goodsSpec = $(this).parent().siblings().eq(6).text();
            object.goodsSpec = goodsSpec;
            object.periodId = periodId;
            tempGoodsSelectedList[goodsId] = object;

            var spanStr = '<span class="label label-default" goodsId="'+goodsId+'">'+goodsName+'<a style="font-size: xx-small" class="removeGoods text-danger"><span class="glyphicon glyphicon-remove"></span></a></span>';
            $("#goodsSelected").append(spanStr);

            delete periodSelectedList[goodsId];//选择商品后，删除临时的时段价格数据
            if(priceTd){
                priceTd.html("未选择");
                priceTd = null;
            }

        }else{
            alertModal(" 该商品已选择");
        }

    });

//    /**
//     * 选择商家
//     */
//    $("#shopsListBody").on("click",".selectShops",function(){
//        var shopId = $(this).parent().siblings().eq(0).text();
//        if(!shopsSelectedList[shopId]){
//            var shopName = $(this).parent().siblings().eq(1).text();
//
//            shopsSelectedList[shopId] = shopName;
//
//            var spanStr = '<span class="label label-default" shopId="'+shopId+'">'+shopName+'<a style="font-size: xx-small" class="removeShops text-danger"><span class="glyphicon glyphicon-remove"></span></a></span>';
//            $("#shopsSelected").append(spanStr);
//        }else{
//            alertModal("该商家已选择");
//        }
//
//    });

    /**
     * 删除已选的商品
     */
    $("#goodsSelected").on("click",".removeGoods",function(){
        var goodsId = $(this).parent().attr("goodsId");
        $(this).parents("span").remove();
        delete tempGoodsSelectedList[goodsId];
    });

    /**
     * 养生、娱乐商家商品价格窗口
     */
    var priceTd = null;//临时保存当前的价格td
    $("#goodsListBody").on("click",".selectPeriod",function(){
        var goodsId = $(this).parent().siblings().eq(0).text();
        priceTd = $(this).parent().siblings().eq(3);
        $("#myModal .modal-body").load("<?php echo U('Admin/Package/getPeriodPrice');?>",{id:goodsId},function(){
            $('#priceTab').TabPanel({tab:0});
            $("#myModal").modal();

        });

    });


    /**
     * 删除已选择的商家
     */
    $("#shopsSelected").on("click",".removeShops",function(){
        var shopId = $(this).parent().attr("shopId");
        $(this).parents("span").remove();
        delete shopsSelectedList[shopId];
    });


    /**
     * 搜索商家
     */

    $("#searchShopBtn").click(function(){
        getShopsListAjax();
    });


    /**
     * 选择价格
     */
    $("#myModal").on("click",".priceTr",function(){
        if($(this).hasClass("bg-info")){
            $(this).removeClass("bg-info");
            $(this).find(".selectedFlag").addClass("hidden");
//            for(var prop in periodSelected){
//                delete periodSelected[prop];
//            }
        }else{
            $(".priceTr").removeClass("bg-info");
            $(this).addClass("bg-info");
            $(".selectedFlag").addClass("hidden");
            $(this).find(".selectedFlag").removeClass("hidden");

        }

    });

    //时段选择
    $("#priceSelect").click(function(){
        var trSelected = $("#myModal .priceTr").filter(".bg-info");
        if(trSelected.length != 0){
            var object = new Object();
            var goodsId = $(".priceTabContent").attr("goodsid");
            object.priceId = trSelected.children().eq(0).text();
            object.periodPrice = trSelected.children().eq(4).text();
            object.weekDate = trSelected.children().eq(5).text();
            object.startTime = trSelected.children().eq(1).text();
            object.endTime = trSelected.children().eq(3).text();
            object.isTom = trSelected.children().eq(2).text();
            periodSelectedList[goodsId] = object;
            priceTd.empty();
            priceTd.append("<div>"+numberToWeek(parseInt(object.weekDate))+"</div><div>"+object.startTime+"-"+object.endTime+"</div><div>"+object.periodPrice+"</div>");
        }
        $("#myModal").modal("hide");
    });

    //打开商品选择窗口
    $("#openGoodsModal").click(function(){
        currentModal = $("#goodsModal");
       $("#goodsModal").modal() ;
    });


    /**
     * 确定添加商品
     */
    $("#addGoodsSelected").click(function(){

        if(!$.isEmptyObject(tempGoodsSelectedList)){
            var listEm = null;
            for(var item in tempGoodsSelectedList){


                listEm  = document.createElement("li");
                $(listEm).addClass("media");
                $(listEm).append( "<a class='pull-left' href='#'> <img class='media-object' src='"+tempGoodsSelectedList[item].goodsThums+"'> </a>");
                var bodyDiv = document.createElement("div");
                $(bodyDiv).addClass("media-body");
                $(bodyDiv).append("<div class='media-heading'>商品名：<span>"+tempGoodsSelectedList[item].goodsName+"</span><a class='removePackageGoods pull-right text-danger'><span class='glyphicon glyphicon-remove'></span></a></div>");
                $(bodyDiv).append("<div class='media-heading'>商品简介：<span>"+tempGoodsSelectedList[item].goodsSpec+"</span></div>");
                $(bodyDiv).append("<div class='media-heading goodsPrice'>商品价格：<span>"+tempGoodsSelectedList[item].goodsPrice+"</span></div>");
                $(bodyDiv).append("<div class='media-heading'>套餐价格：<input type='text' class='tPrice'></div>");



                $(listEm).append(bodyDiv);
                $(listEm).attr("goodsId",item);
                $(listEm).attr("periodId",tempGoodsSelectedList[item].periodId?tempGoodsSelectedList[item].periodId:0);

                $("#tGoodsList").append(listEm);

            }
            goodsSelectedList = $.extend(goodsSelectedList,tempGoodsSelectedList);

            tempGoodsSelectedList = {};

        }
        currentModal = null;
        $("#goodsModal").modal("hide");
        $("#goodsSelected").empty();

    });


    /**
     * 解决子模态框关闭后商品列表模态框
     */
    $("#alertModal").on('shown.bs.modal', function () {
        if(currentModal){
            currentModal.modal("hide");
        }

    });

    $("#alertModal").on('hidden.bs.modal', function () {
        if(currentModal){
            currentModal.modal();
        }

    });

    $("#myModal").on('shown.bs.modal', function () {
        if(currentModal){
            currentModal.modal("hide");
        }

    });

    $("#myModal").on('hidden.bs.modal', function () {
        if(currentModal){
            currentModal.modal();
        }

    });





    //添加、保存套餐
    $("#savePackage").click(function(){


            var flag = true;
            var packageId = "<?php echo ($_GET['id']); ?>";
            if(packageId){
                //修改套餐
                goodsSelectedList = new Object();
                $("#tGoodsList li").each(function(){
                    var object = new Object();
                    var goodsPrice = parseFloat($(this).find(".goodsPrice>span").text());
                    var tPrice = $(this).find("input").val();
                    tPrice = tPrice?(!isNaN(tPrice)?parseFloat(tPrice):0):0;
                    if(tPrice <= 0 || tPrice > goodsPrice){
                        alertModal("套餐价格必须是小于商品价格的正数");
                        flag = false;
                        return false;
                    }else{
                        object.tPrice = tPrice;
                    }
                    var goodsId = $(this).attr("goodsid");
                    object.goodsId=goodsId;
                    object.periodId = $(this).attr("periodId");
                    object.goodsPrice = goodsPrice;
                    goodsSelectedList[goodsId] = object;

                });
            }

            //当套餐Id为空时，为添加套餐，添加的商品不能为空
            if($.isEmptyObject(goodsSelectedList)){
                alertModal("没有添加套餐商品");
                return false;
            }

        //添加套餐
            if(!packageId){
                $("#tGoodsList li").each(function(){
                    var goodsId = $(this).attr("goodsid");
                    var goodsPrice = parseFloat($(this).find(".goodsPrice>span").text());
                    var tPrice = $(this).find("input").val();
                    tPrice = tPrice?(!isNaN(tPrice)?parseFloat(tPrice):0):0;
                    if(tPrice <= 0 || tPrice > goodsPrice){
                        alertModal("套餐价格必须是小于商品价格的正数");
                        flag = false;
                        return false;
                    }else{
                        goodsSelectedList[goodsId]["tPrice"] = tPrice;

                    }

                });
            }

            if(!flag) return false;
            var tStartTime = $("#tStartTime").val();
            var tEndTime = $("#tEndTime").val();
            var tName = $("#tName").val();

            if(tStartTime == '' || tEndTime == ''){
                alertModal("套餐开始或介绍日期不能为空");
                flag == false;
                return false;
            }
            if(tName ==''){
                alertModal("套餐名不能为空");
                return false;
            }






            if(flag){
                Plugins.waitTips({title:'信息提示',content:'正在操作，请稍后...'});
                var packageListUrl = "<?php echo ($packageListUrl); ?>";
                $.post("<?php echo U('Admin/Package/addPackageData');?>",
                        {
                            packageId:packageId,
                            goodsList:goodsSelectedList,
                            tDesc:$("#tDesc").val(),
                            tStartTime:$("#tStartTime").val(),
                            tEndTime:$("#tEndTime").val(),
                            tName:tName
                        },function(data){
                           // alertModal(data.msg);
                            if(data.status=='1'){
                                Plugins.setWaitTipsMsg({content:'操作成功',timeout:1000,callback:function(){
                                    goodsSelectedList = {};
                                    location.href = packageListUrl;
                                }});
                            }else{
                                Plugins.closeWindow();
                                alertModal(data.msg);
                            }
                        }
                );
            }

    });


    //删除套餐商品列表中商品
    $("#tGoodsList").on("click",".removePackageGoods",function(){
        var goodsLi = $(this).parents("li");
        var goodsId = goodsLi.attr("goodsId");
        delete goodsSelectedList[goodsId];
        goodsIdDeleteList.push(goodsId);
        goodsLi.remove();
    });









    $(function(){
        getGoodsListAjax(1);


        <?php if(!empty($areaId1)): ?>getAreaList("areaId2",'<?php echo ($areaId1); ?>',0,'<?php echo ($areaId2); ?>');<?php endif; ?>

    });
</script>
</html>